import 'package:fad/pages/olympic/olympic02.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'olympic01.dart';
import 'olympic02.dart';
import 'olympic03.dart';
import 'olympic04.dart';
import 'olympic05.dart';
import 'olympic06.dart';
import 'olympic07.dart';
import 'olympic08.dart';
import 'olympic09.dart';
import 'olympic10.dart';
import 'olympic11.dart';
import 'olympic12.dart';
import 'olympic13.dart';

class Olympic extends StatefulWidget {
  Olympic({Key? key}) : super(key: key);

  @override
  _OlympicState createState() => _OlympicState();
}

class _OlympicState extends State<Olympic> {
  Widget _page = Center(child: Text('奥运奖牌榜'));
  String _title = '东京奥运会奖牌榜';

  /// 定义省份控制器
  late FixedExtentScrollController pageController;
  int _selected = 0;
  
  @override
  void initState() { 
    super.initState();
    pageController = FixedExtentScrollController(initialItem: 0);
  }

  @override
  Widget build(BuildContext context) {
    List _pages = [
      {
        'title': '请选择',
        'page': Center(child: Text('奥运奖牌榜'))
      },
      {
        'title': '01',
        'page': Olympic01(title: _title)
      },
      {
        'title': '02',
        'page': Olympic02(title: _title)
      },
      {
        'title': '03',
        'page': Olympic03()
      },
      {
        'title': '04',
        'page': Olympic04(title: _title)
      },
      {
        'title': '05',
        'page': Olympic05()
      },
      {
        'title': '06',
        'page': Olympic06(title: _title)
      },
      {
        'title': '07',
        'page': Olympic07(title: _title)
      },
      {
        'title': '08',
        'page': Olympic08()
      },
      {
        'title': '09',
        'page': Olympic09()
      },
      {
        'title': '10',
        'page': Olympic10()
      },
      {
        'title': '11',
        'page': Olympic11(title: _title)
      },
      {
        'title': '12',
        'page': Olympic12()
      },
      {
        'title': '13',
        'page': Olympic13(title: _title)
      }
    ];

    return Row(
      children: [
        Expanded(
          flex: 1,
          child: CupertinoPicker(
            backgroundColor: Colors.white,
            diameterRatio: 1.1,
            scrollController: pageController,
            itemExtent: 48.0,
            onSelectedItemChanged: (position) {
              // print(position);
              setState(() {
                _selected = position;
                _page = _pages[position]['page'];
              });
            },
            children: createEachItem(_pages),
          ),
        ),
        Expanded(
          // It takes 4/5 part of the screen
          flex: 7,
          child: Container(
            margin: EdgeInsets.all(10.0),
            child: _page,
          )
        ),
      ]
    );
  }

  List<Widget> createEachItem(List data) {
    List<Widget> target = [];

    for (Map item in data) {
      target.add(Container(
        padding: EdgeInsets.only(top: 14.0, bottom: 10.0),
        child: Text(
          item['title'] ?? '-',
          style: TextStyle(fontSize: 14.0),
        ),
      ));
    }

    return target;
  }
}